<?php
namespace Admin\Controller;
use Common\Controller\PublicController;

class IndexController extends PublicController {
    //后台首页
    public function index(){
    	// $this->display();
    	// 判断是否登录，登录则进入admin主页，未登录则跳转至登录界面
    	if(session("?admin_name")){
       		$this->display();
    	}else{
    		$this->redirect("Index/login");
    	}
	}
	//登录方法
	public function login(){
		//判断是否接收到数据，未接收到数据则跳转至登录界面
		if(IS_POST){
			//接收到数据，获取post数据并组装成where查询条件
			$where['username']=I('post.username');
			$where['password']=I('post.password');
			$admin=M("Admin");
			$row=$admin->where($where)->find();
			if($row){
				//账户无误，判断是否启用
				if(!$row['state']){
					$this->error("登录失败：账户未启用");
				}
				//登录成功，写入登录次数，写入session，跳转至后台首页
				$data['id']=$row['id'];
				$data['logintimes']=$row['logintimes']+1;
				$admin->save($data);
				session('admin_id',$row['id']);
				session('admin_name',$row['username']);
				$data['status']=1;
				$data['msg']="登录成功，欢迎您{$row['username']}";
				$this->ajaxReturn($data);
				// $this->success("登录成功，欢迎您{$row['username']}",U("Index/index"));
			}else{
				$data['status']=0;
				$data['msg']="账户名或密码错误";
				$this->ajaxReturn($data);
				// $this->error("账户名或密码错误",U("Index/login"));
			}
		}else{
			$this->display();
		}
	}
	//注销方法
	public function logout(){
		$data['id']=session('admin_id');
		$data['lastlogin']=time();
		M('Admin')->save($data);
		session('admin_id',null);
		session('admin_name',null);
		$this->redirect("Index/login");
	}
	//载入头部页面
	public function top(){
		$this->display();
	}

	//载入左侧页面
	public function left(){
		//根据session里的admin_id查询think_admin表获取role_id
		$admin_id=session('admin_id');
		$admin=M('Admin');
		$admin_arr=$admin->find($admin_id);
		$role_id=$admin_arr['role_id'];//得到role_id

		//根据role_id查询think_role表，获取role_name、role_auth_ids
		$role_arr=$admin->table('think_role')->find($role_id);
		$role_name=$role_arr['role_name'];
		$role_auth_ids=$role_arr['role_auth_ids'];
		//通过权限id：role_auth_ids查询think_auth表 获取权限名称auth_arr
		//查询父类
		$map['auth_level']=0;
		//查询子类
		$map1['auth_level']=1;	
		//排除超级管理员
		if($admin_id!=1){
			$map['id']=array('in',$role_auth_ids);
			$map1['id']=array('in',$role_auth_ids);
		}
		$authp=$admin->table('think_auth')->where($map)->select();
		$auths=$admin->table('think_auth')->where($map1)->select();

		//将数据传入模板
		$this->assign('role_name',$role_name);
		$this->assign('authp',$authp);
		$this->assign('auths',$auths);
		$this->display();

	}

    //载入边框页面
    public function swich()
    {
        $this->display();
    }

    //载入右侧主页面
    public function main()
    {
    	$admin_id=session('admin_id');
    	$admin_arr=M('Admin')->find($admin_id);
    	$role_id=$admin_arr['role_id'];
    	$role_name=M('Role')->where(array('id'=>$role_id))->getfield('role_name');
    	$logintimes=$admin_arr['logintimes'];
    	$time=$admin_arr['lastlogin'];
    	$lastlogin=date("Y-m-d H:i:s",$time);
    	$adminCount=M('Admin')->count();
    	$userCount=M('User')->count();
    	//$goodsCount=M('Goods_detail')->count();
    	//$ordersCount=M('Orders')->count();

    	$this->assign('role_name',$role_name);
    	$this->assign('logintimes',$logintimes);
    	$this->assign('lastlogin',$lastlogin);
    	$this->assign('role_name',$role_name);
    	$this->assign('adminCount',$adminCount);
    	$this->assign('userCount',$userCount);
    	//$this->assign('goodsCount',$goodsCount);
    	//$this->assign('ordersCount',$ordersCount);
        $this->display();
    }

    //载入底部页面
    // public function bottom()
    // {
    //     $this->display();
    // }
}